package com.sunshine.student_register.ui.activity.manager;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Database;

import com.google.android.material.card.MaterialCardView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.sunshine.student_register.R;
import com.sunshine.student_register.database.register.RegisterEntity;
import com.sunshine.student_register.database.student.StudentEntity;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author sunshine
 * @date 2020/5/30
 * 回收布局适配器
 */
public class StudentRecyclerAdapter extends RecyclerView.Adapter<StudentRecyclerAdapter.StudentRecyclerViewHolder> {

    private ArrayList<StudentEntity> studentEntityArrayList;
    private Context context;
    private CallBack callBack;

    public StudentRecyclerAdapter(ArrayList<StudentEntity> studentEntityArrayList, CallBack callBack) {
        this.studentEntityArrayList = studentEntityArrayList;
        this.callBack = callBack;
    }

    @NonNull
    @Override
    public StudentRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        return new StudentRecyclerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull StudentRecyclerViewHolder holder, int position) {
        final StudentEntity entity = studentEntityArrayList.get(position);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
                builder.setTitle("详细信息");
                builder.setMessage("学生ID：" + entity.getId() +
                        "\n学生姓名：" + entity.getName() +
                        "\n学生年龄：" + entity.getOld() +
                        "\n学生专业：" + entity.getDepartment() +
                        "\n学生入学年：" + entity.getStartYear());
                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        entity.setName("修改名称");
                        callBack.callBack(entity, 0);
                    }
                });
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callBack.callBack(entity, 1);
                    }
                });
                builder.create().show();
            }
        });
        holder.studentId.setText(String.valueOf(entity.getId()));
        holder.name.setText("学生ID：" + entity.getName());
        holder.other.setText("学生专业：" + entity.getDepartment());
    }

    @Override
    public int getItemCount() {
        return studentEntityArrayList.size();
    }

    class StudentRecyclerViewHolder extends RecyclerView.ViewHolder{

        MaterialCardView cardView;
        TextView studentId, name, other;

        StudentRecyclerViewHolder(@NonNull View itemView) {
            super(itemView);

            cardView = itemView.findViewById(R.id.cardview);
            studentId = itemView.findViewById(R.id.textView_id);
            name = itemView.findViewById(R.id.textView_name);
            other = itemView.findViewById(R.id.textView_other);
        }
    }
}
